<template>
  <view class="">
    <view class="indexeditlist">
      <view class="indexedititem">
        <text>分类</text>
        <u-select
          @confirm="confirm"
          mode="single-column"
          v-model="show1"
          :list="categoryList"
        ></u-select>
        <text>{{ name }}</text>
        <u-button v-model="show1" @click="selectParent">请选择</u-button>
      </view>

      <view class="indexedititem">
        <text>标题</text>
        <input type="text" placeholder="请输入标题" v-model="title" />
      </view>
      <view class="indexedititem">
        <text>货号</text>
        <input type="text" placeholder="请输入货号" v-model="huohao" />
      </view>
      <view class="indexedititem">
        <text>成本价</text>
        <input type="number" placeholder="请输入成本价" v-model="constprice" />
      </view>
      <view class="indexedititem">
        <text>售价</text>
        <input type="number" placeholder="请输入售价" v-model="price" />
      </view>
      <!-- 新增会员价 -->
      <view class="indexedititem">
        <text>会员价</text>
        <input type="number" placeholder="请输入会员价" v-model="vipprice" />
      </view>

      <!-- 选择商品类别 -->
      <view class="indexedititem">
        <text>商品类别</text>
        <u-select
          @confirm="confirmType"
          mode="single-column"
          v-model="show2"
          :list="goodstypelist"
        ></u-select>
        <text>{{ typename }}</text>
        <u-button v-model="show2" @click="selectType">请选择</u-button>
      </view>

      <!-- 选择商品单位 -->
      <view class="indexedititem">
        <text>商品单位</text>
        <u-select
          @confirm="confirmUnit"
          mode="single-column"
          v-model="show3"
          :list="goodsunit"
        ></u-select>
        <text>{{ unitname }}</text>
        <u-button v-model="show3" @click="selectUnit">请选择</u-button>
      </view>
    </view>
    <view class="indexbtn" @click="handleAdd"> 提交 </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 标题
      title: "",
      // 货号
      huohao: "",
      // 成本价
      constprice: "",
      // 售价
      price: "",
      // 会员价
      vipprice: "",

      // 分类
      show1: false,
      //   类别
      show2: false,
      //   单位
      show3: false,
      // 分类下拉
      categoryList: [],

      //   选中的name值
      name: "",
      //   选中的id
      id: "",

      //   商品类别下拉
      goodstypelist: [],
      // 选中的名字
      typename: "",
      // 选中的value
      typeid: "",
      // 商品单位下拉
      goodsunit: [],
      // 选中的名字
      unitname: "",
      // 选中的value
      unitid: "",
    };
  },
  onLoad() {},
  methods: {
    //   提交添加事件
    handleAdd() {
      console.log("提交添加");
      uni.request({
        url: "http://gxyckf.1qit.com/api/admin.goods/add", //仅为示例，并非真实接口地址。
        data: {
          category: this.id,
          title: this.title,
          art_no: this.huohao,
          const_price: this.constprice,
          price: this.price,
          color: this.typeid,
          goods_unit: this.unitname,
          // 会员价
          vip_rice: this.vipprice,
        },
        method: "POST",

        header: {
          token: uni.getStorageSync("token"),
        },
        success: (res) => {
          if (res.data.code == 0) {
            uni.showToast({
              title: res.data.msg,
              icon: "error",
              duration: 2000,
            });
          } else if (res.data.code == 1) {
            uni.showToast({
              title: res.data.msg,
              icon: "success",
              duration: 1500,
            });
            // 返回主页
            uni.navigateBack({
              url: "../goodsadmin/goodsadmin",
            });
          }
        },
      });
    },
    //   打开选择父级
    selectParent() {
      this.show1 = true;
      //   查询父商品列表
      uni.request({
        url: "http://gxyckf.1qit.com/api/admin.goods/categoryList", //仅为示例，并非真实接口地址。
        data: {},
        method: "POST",

        header: {
          token: uni.getStorageSync("token"),
        },
        success: (res) => {
          if (res.data.code == 1) {
            var list = [];
            for (var i in res.data.data) {
              var obj = {};
              obj.value = res.data.data[i].id;
              obj.label = res.data.data[i].name;
              list.push(obj);
            }
            this.categoryList = list;
          }
        },
      });
    },
    //   选择商品父级
    confirm(e) {
      console.log(e);
      e.map((item) => {
        // console.log(item);
        this.name = item.label;
        this.id = item.value;
      });
    },

    // 选择类别
    //   打开选择父级
    selectType() {
      this.show2 = true;
      //   查询父商品列表
      uni.request({
        url: "http://gxyckf.1qit.com/api/admin.goods/getValues", //仅为示例，并非真实接口地址。
        data: {},
        method: "POST",

        header: {
          token: uni.getStorageSync("token"),
        },
        success: (res) => {
          if (res.data.code == 1) {
            console.log(res.data.data.color);
            // 将对象转换成数组
            var typearr = [];
            typearr = Object.keys(res.data.data.color).map((val) => ({
              value: val,
              label: res.data.data.color[val],
            }));
            console.log(typearr);
            this.goodstypelist = typearr;
          }
        },
      });
    },
    //   选择商品父级
    confirmType(e) {
      console.log(e);
      e.map((item) => {
        // console.log(item);
        this.typename = item.label;
        this.typeid = item.value;
      });
    },

    // 选择单位
    //   打开选择父级
    selectUnit() {
      this.show3 = true;
      //   查询父商品列表
      uni.request({
        url: "http://gxyckf.1qit.com/api/admin.goods/getValues", //仅为示例，并非真实接口地址。
        data: {},
        method: "POST",

        header: {
          token: uni.getStorageSync("token"),
        },
        success: (res) => {
          if (res.data.code == 1) {
            console.log(res.data.data.goods_unit);
            // 将对象转换成数组
            var unitarr = [];
            unitarr = Object.keys(res.data.data.goods_unit).map((val) => ({
              value: val,
              label: res.data.data.goods_unit[val],
            }));
            console.log(unitarr);
            this.goodsunit = unitarr;
          }
        },
      });
    },
    //   选择商品父级
    confirmUnit(e) {
      console.log(e);
      e.map((item) => {
        // console.log(item);
        this.unitname = item.label;
        this.unitid = item.value;
      });
    },
  },
  mounted() {
    //   初始查询分类列表
    uni.request({
      url: "http://gxyckf.1qit.com/api/admin.goods/categoryList", //仅为示例，并非真实接口地址。
      data: {},
      method: "POST",

      header: {
        token: uni.getStorageSync("token"),
      },
      success: (res) => {
        if (res.data.code == 1) {
          var list = [];
          for (var i in res.data.data) {
            var obj = {};
            obj.value = res.data.data[i].id;
            obj.label = res.data.data[i].name;
            list.push(obj);
          }
          this.categoryList = list;
        }
      },
    });
    // 初始查询类别
    uni.request({
      url: "http://gxyckf.1qit.com/api/admin.goods/getValues", //仅为示例，并非真实接口地址。
      data: {},
      method: "POST",

      header: {
        token: uni.getStorageSync("token"),
      },
      success: (res) => {
        if (res.data.code == 1) {
          console.log(res.data.data.color);
          // 将对象转换成数组
          var typearr = [];
          typearr = Object.keys(res.data.data.color).map((val) => ({
            value: val,
            label: res.data.data.color[val],
          }));
          console.log(typearr);
          this.goodstypelist = typearr;
        }
      },
    });
    // 初始查询单位
    uni.request({
      url: "http://gxyckf.1qit.com/api/admin.goods/getValues", //仅为示例，并非真实接口地址。
      data: {},
      method: "POST",

      header: {
        token: uni.getStorageSync("token"),
      },
      success: (res) => {
        if (res.data.code == 1) {
          console.log(res.data.data.goods_unit);
          // 将对象转换成数组
          var unitarr = [];
          unitarr = Object.keys(res.data.data.goods_unit).map((val) => ({
            value: val,
            label: res.data.data.goods_unit[val],
          }));
          console.log(unitarr);
          this.goodsunit = unitarr;
        }
      },
    });
  },
};
</script>

<style>
.indexeditlist {
  width: 90%;
  margin: 4% 5%;
}
.indexedititem {
  border-bottom: 1px solid #e6e6e6;
  padding: 5% 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.indexedititem text {
  width: 160rpx;
  color: #313131;
}
.indexedititem input {
  font-size: 28rpx;
  width: 500rpx;
  color: #828282;
  text-align: right;
}
.indexbtn {
  width: 80%;
  margin: 2% 10%;
  text-align: center;
  background: red;
  font-size: 32rpx;
  line-height: 3;
  color: #fff;
  border-radius: 60rpx;
  margin-top: 100rpx;
}
.u-icon__icon {
  left: 44%;
}
.u-hairline-border:after {
  border: 0 !important;
}
.specifications_list text {
  margin-right: 2%;
  color: #5d5d5d;
  font-size: 28rpx;
}
</style>
